﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace Light.Utility
{
    /// <summary>
    /// 序列化与反序列化器
    /// </summary>
    public class Serializer
    {
        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] Serialize<T>(T obj)
        {
            MemoryStream ms = new MemoryStream();
            IFormatter IF = new BinaryFormatter();
            try
            {
                IF.Serialize(ms, obj);
                return ms.ToArray();

            }
            catch (Exception ex)
            {

                throw ex;
            }


        }

        /// <summary>
        /// 反序列化对象
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static T Deserialize<T>(byte[] data)
        {
            MemoryStream ms = new MemoryStream(data);
            IFormatter IF = new BinaryFormatter();
            try
            {
                return (T)IF.Deserialize(ms);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] Serialize(object obj)
        {
            return Serialize<object>(obj);
        }

        /// <summary>
        /// 反序列化
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static object Deserialize(byte[] data)
        {
            return Deserialize<object>(data);
        }

        /// <summary>
        /// 创建对象的MD5值
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string CreateMD5(object obj)
        {
            string str = null;
            try
            {
                System.Security.Cryptography.MD5CryptoServiceProvider md5Provider = new System.Security.Cryptography.MD5CryptoServiceProvider();
                byte[] datas = Serialize(obj);

                str = BitConverter.ToString(md5Provider.ComputeHash(datas));
                if (!String.IsNullOrWhiteSpace(str))
                {
                    str = str.Replace("-", "").ToLower();
                }
            }
            catch
            {

            }
            return str;
        }

        /// <summary>
        /// 创建字符串的MD5值
        /// </summary>
        /// <param name="input"></param>
        /// <param name="encoding">编码方式</param>
        /// <returns></returns>
        public static string CreateMD5(string input, Encoding encoding)
        {
            StringBuilder pwd = new StringBuilder();
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] s = md5.ComputeHash(encoding.GetBytes(input));
            for (int i = 0; i < s.Length; i++)
            {
                pwd.Append(s[i].ToString("x").PadLeft(2, '0'));
            }
            return pwd.ToString();
        }


        /// <summary>
        ///创建字符串的MD5值(UTF8编码)
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string CreateMD5(string input)
        {
            StringBuilder pwd = new StringBuilder();
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
            for (int i = 0; i < s.Length; i++)
            {
                pwd.Append(s[i].ToString("x").PadLeft(2, '0'));
            }
            return pwd.ToString();
        }
    }
}
